<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>播放语音</title>
	</head>
	<body>
		<input id="money" type="number">
		<button onclick="readMoney($('#money').val())" type="button">开始念</button>
	</body>
	<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
	<script>
		$(function() {
			//可播报队列，播报过程中有新的播报任务，会在播报完成后继续执行。
		});
		
		var _taskpool = [];//播报的任务池
		var _reading = false;//播报状态
		var _voicepath = 'voice/';//播报音源的目录
		var _voicelist_final = [];//单个最终的播报语音列表
		
		function readMoney(moneystr) {//入口方法
			_taskpool.push(moneystr);
			var seachVoiceEnable = setInterval(function(){
				if(!_reading){
					if(_taskpool.length>0){
						initSource(_taskpool[0]);
						_taskpool.splice(0, 1);
					}else{
						clearInterval(seachVoiceEnable);
					}
				}
			},1000);
		}
		
		function initSource(money) {//排版需要播报的语音，并且先读‘收款成功’
			_reading = true;
			var voicelist = [];
			path = 'voice/';
			var singleMapping = {
				'0': 'tts_0.mp3',
				'1': 'tts_1.mp3',
				'2': 'tts_2.mp3',
				'3': 'tts_3.mp3',
				'4': 'tts_4.mp3',
				'5': 'tts_5.mp3',
				'6': 'tts_6.mp3',
				'7': 'tts_7.mp3',
				'8': 'tts_8.mp3',
				'9': 'tts_9.mp3'
			};
			var unitMapping = {
				'shi': 'tts_ten.mp3',
				'bai': 'tts_hundred.mp3',
				'qian': 'tts_thousand.mp3',
				'wan': 'tts_ten_thousand.mp3',
				'dian': 'tts_dot.mp3'
			};
			// money = '7893.00';
			//1.去除.00不念
			if (money.indexOf(".00") != -1) {
				money = money.split('.00').join('');
				console.log('筛除.00后：' + money)
			}
			//2.判断整数位数，不同位数念法不同
			var whole = money.split('.')[0];
			if (whole.length == 1) {
				voicelist.push(singleMapping[whole]);
			} else if (whole.length == 2) {
				var z1 = whole[0];
				var z2 = whole[1];
				if (z1 != '1') {
					voicelist.push(singleMapping[z1]);
				}
				voicelist.push(unitMapping['shi']);
				if (z2 != '0') {
					voicelist.push(singleMapping[z2]);
				}
			} else if (whole.length == 3) {
				var z1 = whole[0];
				var z2 = whole[1];
				var z3 = whole[2];
				voicelist.push(singleMapping[z1]);
				voicelist.push(unitMapping['bai']);
				if (!(z2 == 0 && z3 == 0)) {
					voicelist.push(singleMapping[z2]);
					if (z2 != '0') {
						voicelist.push(unitMapping['shi']);
					}
					if (z3 != '0') {
						voicelist.push(singleMapping[z3]);
					}
				}
			} else if (whole.length == 4) {
				var z1 = whole[0];
				var z2 = whole[1];
				var z3 = whole[2];
				var z4 = whole[3];
				voicelist.push(singleMapping[z1]);
				voicelist.push(unitMapping['qian']);
				if (!(z2 == 0 && z3 == 0 && z4 == 0)) {
					if (z2 == 0 && z3 == 0) {
						voicelist.push(singleMapping[0]);
					} else {
						voicelist.push(singleMapping[z2]);
						if (z2 != '0') {
							voicelist.push(unitMapping['bai']);
						}
						if (!(z3 == '0' && z4 == '0')) {
							voicelist.push(singleMapping[z3]);
						}
						if (z3 != '0') {
							voicelist.push(unitMapping['shi']);
						}
					}
					if (z4 != '0') {
						voicelist.push(singleMapping[z4]);
					}
				}
			} else if (whole.length == 5) {
				var z1 = whole[0];
				var z2 = whole[1];
				var z3 = whole[2];
				var z4 = whole[3];
				var z5 = whole[4];
				voicelist.push(singleMapping[z1]);
				voicelist.push(unitMapping['wan']);
				if (!(z2 == 0 && z3 == 0 && z4 == 0 && z5 == 0)) {
					voicelist.push(singleMapping[z2]);
					if (z2 != '0') {
						voicelist.push(unitMapping["qian"]);
					}
					voicelist.push(singleMapping[z3]);
					if (z3 != '0') {
						voicelist.push(unitMapping["bai"]);
					}
					if (!(z4 == '0' && z5 == '0')) {
						voicelist.push(singleMapping[z4]);
					}
					if (z4 != '0') {
						voicelist.push(unitMapping["shi"]);
					}
					if (z5 != '0') {
						voicelist.push(singleMapping[z5]);
					}
				}
			}
			_voicelist_final = [];
			for (var n in voicelist) {
				var v = voicelist[n];
				var vv = voicelist[Number(n) + 1];
				if (v == 'tts_0.mp3' && vv == 'tts_0.mp3') {
					continue;
				}
				_voicelist_final.push(v);
			}
			//3.念小数点后
			if (money.split('.').length > 1) {
				_voicelist_final.push(unitMapping['dian']);
				var jiaofen = money.split('.')[1];
				if (jiaofen.length == 1) {
					_voicelist_final.push(singleMapping[jiaofen]);
				} else {
					_voicelist_final.push(singleMapping[jiaofen[0]]);
					if (jiaofen[1] != '0') {
						_voicelist_final.push(singleMapping[jiaofen[1]]);
					}
				}
			}
			_voicelist_final.push('tts_yuan.mp3');
			$('body').append('<iframe id="id_start" class="voiceControl" style="display:none" src="' + _voicepath +
				'tts_success.mp3"></iframe>');
			var iframe = document.getElementById('id_start');
			if (iframe.attachEvent) {
				iframe.attachEvent("onload", function() {
					//iframe加载完成后你需要进行的操作
					setTimeout(function(){
						readnum();
					},1400);
				});
			} else {
				iframe.onload = function() {
					//iframe加载完成后你需要进行的操作 
					setTimeout(function(){
						readnum();
					},1400);
				};
			}
		}

		function readnum() {//读金额
			function dealNext(){
				setTimeout(function(){
					_voicelist_final.splice(0, 1);
					readnum();
				},700);
			}
			if (_voicelist_final.length > 0) {
				$('body').append('<iframe id="id_'+_voicelist_final.length+'" class="voiceControl" style="display:none" src="' + _voicepath + _voicelist_final[0] +
					'"></iframe>');
				var iframe = document.getElementById('id_'+_voicelist_final.length);
				if (iframe.attachEvent) {
					iframe.attachEvent("onload", function() {
						//iframe加载完成后你需要进行的操作
						dealNext();
					});
				} else {
					iframe.onload = function() {
						//iframe加载完成后你需要进行的操作 
						dealNext();
					};
				}
			}else{
				$('.voiceControl').remove();
				_reading = false;
			}
		}
	</script>
</html>
